package com.pocketsweet.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.MLApplication;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.ui.uilib.MyAlertDialog;

@ContentView(R.layout.activity_applytest)
public class ApplyTest extends BaseActivity {

	private MLUser curUser;

	@ViewInject(R.id.testHeader)
	private HeaderLayout testHeader;

	@ViewInject(R.id.rlIntro)
	private RelativeLayout rlIntro;

	@ViewInject(R.id.tvPre)
	private TextView tvPre;

	@ViewInject(R.id.tvNext)
	private TextView tvNext;

	@ViewInject(R.id.llIntroStep1)
	private LinearLayout llIntroStep1;

	@ViewInject(R.id.llIntroStep2)
	private LinearLayout llIntroStep2;

	@ViewInject(R.id.llIntroStep3)
	private LinearLayout llIntroStep3;

	@ViewInject(R.id.llIntroStep4)
	private LinearLayout llIntroStep4;

	@ViewInject(R.id.rlWelcome)
	private RelativeLayout rlWelcome;

	@ViewInject(R.id.ivAvatar)
	private ImageView ivAvatar;

	@ViewInject(R.id.ivUserAvatar)
	private ImageView ivUserAvatar;

	@ViewInject(R.id.tvName)
	private TextView tvName;

	@ViewInject(R.id.btn_begin)
	private Button btn_begin;

	@ViewInject(R.id.btn_test)
	private Button btn_test;

	@ViewInject(R.id.rlTest)
	private RelativeLayout rlTest;

	@ViewInject(R.id.tvA)
	private TextView tvA;

	@ViewInject(R.id.tvB)
	private TextView tvB;

	@ViewInject(R.id.rlErrTip)
	private RelativeLayout rlErrTip;

	@ViewInject(R.id.llTestContent1)
	private LinearLayout llTestContent1;

	@ViewInject(R.id.llTestContent2)
	private LinearLayout llTestContent2;

	@ViewInject(R.id.llTestContent3)
	private LinearLayout llTestContent3;

	@ViewInject(R.id.llTestContent4)
	private LinearLayout llTestContent4;

	@ViewInject(R.id.llTestContent5)
	private LinearLayout llTestContent5;

	@ViewInject(R.id.llTestContent6)
	private LinearLayout llTestContent6;

	@ViewInject(R.id.llTestContent7)
	private LinearLayout llTestContent7;

	@ViewInject(R.id.llTestContent8)
	private LinearLayout llTestContent8;

	@ViewInject(R.id.llTestContent9)
	private LinearLayout llTestContent9;

	@ViewInject(R.id.llTestContent10)
	private LinearLayout llTestContent10;

	@ViewInject(R.id.llTestContent11)
	private LinearLayout llTestContent11;

	@ViewInject(R.id.llTestContent12)
	private LinearLayout llTestContent12;

	@ViewInject(R.id.llTestContent13)
	private LinearLayout llTestContent13;

	@ViewInject(R.id.llTestContent14)
	private LinearLayout llTestContent14;

	@ViewInject(R.id.llTestContent15)
	private LinearLayout llTestContent15;

	@ViewInject(R.id.rlSucessTip)
	private RelativeLayout rlSucessTip;

	private boolean isBegin = false;// 是否点击了开始测试
	private boolean isFinish = false;// 测试是否已经完成

	private int introIndex = 1;
	private int testIndex = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		curUser = UserService.getCurrentUser();
		// 初始化header
		initHeader();
		// 初始化欢迎页面
		initWelcome();
		// 初始介绍
		initIntro();
		// 初始化考试题
		initTest();
	}

	private void initHeader() {
		testHeader.init(HeaderStyle.IMG_TITLE_TEXT);
		testHeader.setRightText("");
		testHeader.setMiddleText("测试");
		testHeader.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				if (!isBegin) {
					ApplyTest.this.finish();
					return;
				}
				if (isFinish == true) {
					Identification.setFinishTest();
					ApplyTest.this.finish();
				} else {
					final MyAlertDialog dialog = new MyAlertDialog(
							ApplyTest.this).builder()
							.setMsg("如果放弃，此次的考试将不会保存，确定放弃？").setTitle("提示")
							.setNegativeButton("取消", new OnClickListener() {
								@Override
								public void onClick(View v) {

								}
							});
					dialog.setPositiveButton("确定", new OnClickListener() {
						@SuppressLint("SimpleDateFormat")
						@Override
						public void onClick(View v) {
							ApplyTest.this.finish();
						}
					});
					dialog.show();

				}
			}
		});
	}

	private void initWelcome() {
		// 设置头像
		UserService.displayAvatar(curUser.getAvatarUrl(), ivAvatar);
		UserService.displayAvatar(curUser.getAvatarUrl(), ivUserAvatar);
		// 设置姓名
		tvName.setText(curUser.getNickname());
		// 设置点击开始的事件
		btn_begin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 点击开始的事件
				Animation anim = AnimationUtils.loadAnimation(
						MLApplication.getContext(), R.anim.fade_out);
				anim.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						rlWelcome.setVisibility(View.GONE);
						rlIntro.setVisibility(View.VISIBLE);
						testHeader.setMiddleText("介绍");
						setRightText(introIndex, 0);
					}
				});
				rlWelcome.startAnimation(anim);
			}
		});
	}

	private void setRightText(int index, int type) {
		// type 0 设置5，type 1 设置15
		if (type == 0) {
			testHeader.setRightText(index + "/4");
		} else {
			testHeader.setRightText(index + "/15");
		}
	}

	private void initIntro() {
		tvPre.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (introIndex == 1) {
					return;
				}
				if (introIndex == 2) {
					llIntroStep1.setVisibility(View.VISIBLE);
					llIntroStep2.setVisibility(View.GONE);
				}
				if (introIndex == 3) {
					llIntroStep2.setVisibility(View.VISIBLE);
					llIntroStep3.setVisibility(View.GONE);
				}

				if (introIndex == 4) {
					llIntroStep3.setVisibility(View.VISIBLE);
					llIntroStep4.setVisibility(View.GONE);
				}

				introIndex--;
				setRightText(introIndex, 0);
			}
		});

		tvNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (introIndex == 5) {
					return;
				}
				if (introIndex == 1) {
					llIntroStep2.setVisibility(View.VISIBLE);
					llIntroStep1.setVisibility(View.GONE);
				}
				if (introIndex == 2) {
					llIntroStep3.setVisibility(View.VISIBLE);
					llIntroStep2.setVisibility(View.GONE);
				}

				if (introIndex == 3) {
					llIntroStep4.setVisibility(View.VISIBLE);
					llIntroStep3.setVisibility(View.GONE);
					tvNext.setVisibility(View.GONE);
					tvPre.setVisibility(View.GONE);
					btn_test.setVisibility(View.VISIBLE);
					rlWelcome.setVisibility(View.GONE);
				}
				introIndex++;
				setRightText(introIndex, 0);
			}
		});

		btn_test.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				rlIntro.setVisibility(View.GONE);
				rlTest.setVisibility(View.VISIBLE);
				testHeader.setMiddleText("测试题");
				setRightText(testIndex, 1);
				isBegin = true;
			}
		});
	}

	private void initTest() {
		// 选b 的是 2 9 11 13
		tvA.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (testIndex != 2 && testIndex != 9 && testIndex != 11
						&& testIndex != 13) {
					if (testIndex == 1) {
						llTestContent1.setVisibility(View.GONE);
						llTestContent2.setVisibility(View.VISIBLE);
					}
					if (testIndex == 3) {
						llTestContent3.setVisibility(View.GONE);
						llTestContent4.setVisibility(View.VISIBLE);
					}
					if (testIndex == 4) {
						llTestContent4.setVisibility(View.GONE);
						llTestContent5.setVisibility(View.VISIBLE);
					}

					if (testIndex == 5) {
						llTestContent5.setVisibility(View.GONE);
						llTestContent6.setVisibility(View.VISIBLE);
					}
					if (testIndex == 6) {
						llTestContent6.setVisibility(View.GONE);
						llTestContent7.setVisibility(View.VISIBLE);
					}
					if (testIndex == 7) {
						llTestContent7.setVisibility(View.GONE);
						llTestContent8.setVisibility(View.VISIBLE);
					}
					if (testIndex == 8) {
						llTestContent8.setVisibility(View.GONE);
						llTestContent9.setVisibility(View.VISIBLE);
					}
					if (testIndex == 10) {
						llTestContent10.setVisibility(View.GONE);
						llTestContent11.setVisibility(View.VISIBLE);
					}
					if (testIndex == 12) {
						llTestContent12.setVisibility(View.GONE);
						llTestContent13.setVisibility(View.VISIBLE);
					}
					if (testIndex == 14) {
						llTestContent14.setVisibility(View.GONE);
						llTestContent15.setVisibility(View.VISIBLE);
					}
					if (testIndex == 15) {
						llTestContent15.setVisibility(View.GONE);
						tvA.setVisibility(View.GONE);
						tvB.setVisibility(View.GONE);
						// 完成的显示
						isFinish = true;
						rlSucessTip.setVisibility(View.VISIBLE);
						testHeader.setRightText("完成");
						testHeader
								.setRighttContainerClickListener(new onRightContainerListener() {

									@Override
									public void onClick() {
										// 提交
										Identification.setFinishTest();
										ApplyTest.this.finish();
									}
								});
					}
					testIndex++;
					if (testIndex != 16) {
						setRightText(testIndex, 1);
					}

				} else {
					// 弹出框
					rlErrTip.setVisibility(View.VISIBLE);
				}
			}
		});

		tvB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (testIndex == 2 || testIndex == 9 || testIndex == 11
						|| testIndex == 13) {
					if (testIndex == 2) {
						llTestContent2.setVisibility(View.GONE);
						llTestContent3.setVisibility(View.VISIBLE);
					}
					if (testIndex == 9) {
						llTestContent9.setVisibility(View.GONE);
						llTestContent10.setVisibility(View.VISIBLE);
					}
					if (testIndex == 11) {
						llTestContent11.setVisibility(View.GONE);
						llTestContent12.setVisibility(View.VISIBLE);
					}

					if (testIndex == 13) {
						llTestContent13.setVisibility(View.GONE);
						llTestContent14.setVisibility(View.VISIBLE);
					}
					testIndex++;
					setRightText(testIndex, 1);
				} else {
					// 弹出框
					rlErrTip.setVisibility(View.VISIBLE);
				}

			}
		});

		rlErrTip.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rlErrTip.setVisibility(View.GONE);
			}
		});
	}

	/**
	 * 重写onKeyDown方法
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isFinish == true) {
				Identification.setFinishTest();
				ApplyTest.this.finish();
			} else {
				final MyAlertDialog dialog = new MyAlertDialog(ApplyTest.this)
						.builder().setMsg("如果放弃，此次的考试将不会保存，确定放弃？")
						.setTitle("提示")
						.setNegativeButton("取消", new OnClickListener() {
							@Override
							public void onClick(View v) {

							}
						});
				dialog.setPositiveButton("确定", new OnClickListener() {
					@SuppressLint("SimpleDateFormat")
					@Override
					public void onClick(View v) {
						ApplyTest.this.finish();
					}
				});
				dialog.show();

			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

}
